import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';
import 'package:kq_flutter_widgets/widgets/animateTitle/kq_animate_title.dart';
import 'package:kq_flutter_widgets/widgets/animateListView/kq_animate_list_view.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_small_button.dart';
import 'package:kq_flutter_widgets/widgets/dropDownMenu/kq_drop_down_menu.dart';
import 'package:kq_flutter_widgets/widgets/emptyView/empty_view.dart';
import 'package:kq_flutter_widgets/widgets/formItem/entity/kq_form_entity.dart';
import 'package:kq_flutter_widgets/widgets/formItem/kq_header_item_view.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';
import 'package:kq_flutter_widgets/widgets/update/update_view.dart';

import '../../resources/Images.dart';

class KqAnimateDemo extends StatefulWidget {
  const KqAnimateDemo({super.key});

  @override
  State<StatefulWidget> createState() => KqAnimateDemoState();
}

class KqAnimateDemoState extends State<KqAnimateDemo> {
  int type = 0;
  UpdateViewState? updateViewState;
  int index = 0;

  List<List<String>> data = [
    [
      "Test11",
      "Test12",
      "Test13",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
    ],
    [
      "Test21",
      "Test22",
      "Test23",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
    ],
    [
      "Test31",
      "Test32",
      "Test33",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
      "Test3",
    ],
    [
      "Test41",
    ],
    []
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: '首页动画演示',
        back: () {
          Get.back();
        },
      ),
      body: Column(
        children: [
          KqHeaderItemView(
              entity: KqFormEntity(title: 'KqAnimateTitle科强首页动画title')),
          KqAnimateTitle(
            data: [
              KqAnimateEntity(
                  title: "首页首页",
                  count: "99",
                  url: Images.demoIcLan2,
                  url2: Images.demoIcLan,
                  isShowRightArrow: true),
              KqAnimateEntity(
                title: "工作",
                count: "0",
                url: Images.demoIcLv2,
                url2: Images.demoIcLv,
              ),
              KqAnimateEntity(
                title: "空闲",
                count: "0",
                url: Images.demoIcHuang2,
                url2: Images.demoIcHuang,
              ),
              KqAnimateEntity(
                title: "故障",
                count: "0",
                url: Images.demoIcHong2,
                url2: Images.demoIcHong,
              ),
              KqAnimateEntity(
                title: "离线",
                count: "0",
                url: Images.demoIcHui2,
                url2: Images.demoIcHui,
              ),
            ],
            onTap: (index) {
              print("onTap index=$index");
              this.index = index;
              setState(() {});
            },
            onTapPop: (index, context) {
              KqDropDownMenu.showPopMenu(
                context: context!,
                menuData: [
                  KqDropMenuData("全部设备"),
                  KqDropMenuData("注塑车间"),
                  KqDropMenuData("cnc车间"),
                  KqDropMenuData("组装车间"),
                ],
                offsetX: 10,
              );
            },
          ),
          KqHeaderItemView(
              entity: KqFormEntity(title: 'KqAnimateListView科强首页列表动画')),
          Row(
            children: [
              const SizedBox(width: 20),
              KqSmallButton(
                title: '水平左边动画',
                onTap: (disabled) {
                  type = 0;
                  updateViewState?.update();
                },
              ),
              const SizedBox(width: 20),
              KqSmallButton(
                title: '水平右边动画',
                onTap: (disabled) {
                  type = 1;
                  updateViewState?.update();
                },
              ),
              const SizedBox(width: 20),
              KqSmallButton(
                title: '放大动画',
                onTap: (disabled) {
                  type = 2;
                  updateViewState?.update();
                },
              ),
            ],
          ),
          Expanded(
            child: UpdateView(
              build: (UpdateViewState state) {
                updateViewState = state;
                EasyRefreshController controller =
                    EasyRefreshController(controlFinishRefresh: true);
                return EasyRefresh(
                  controller: controller,
                  onRefresh: () => controller.finishRefresh(),
                  child: Stack(
                    children: [
                      Visibility(
                        visible: data[index].isNotNullOrEmpty,
                        maintainState: true,
                        child: KqAnimateListView(
                          data: data[index],
                          item: (t) {
                            return Container(
                              width: double.infinity,
                              height: 50,
                              color: Colors.redAccent,
                              margin: EdgeInsets.only(top: 10.r),
                              child: Text(t),
                            );
                          },
                          animate: type == 0
                              ? HorizontalAnimate(
                                  direction: HorizontalAnimateDirection.left)
                              : type == 1
                                  ? HorizontalAnimate(
                                      direction:
                                          HorizontalAnimateDirection.right)
                                  : ScaleAnimate(),
                          isNeedFlashEveryTime: true,
                        ),
                      ),
                      Visibility(
                        visible: !data[index].isNotNullOrEmpty,
                        maintainState: true,
                        child: const KqEmptyView(),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
